/*
 * @Author: 哈兰德
 */
import { LOG_DIR,stgEnv } from '@/config/default'

const LAYOUT_ERROR = {
  type: 'pattern',
  pattern:'%d{yyyy-MM-dd hh:mm:ss.SSS} %p [%X{stgEnv}] [%X{userNo}] [%X{userAgent}] : %m%n'
  // pattern: '[%d{yyyy-MM-dd hh:mm:ss}] [%p] -- %m %n userNo=%X{userNo} %n userAgent=%X{userAgent}'
}
const LAYOUT = {
  type: 'pattern',
  // pattern: '[%d{yyyy-MM-dd hh:mm:ss}] [%p] -- %m'
  pattern:'%d{yyyy-MM-dd hh:mm:ss.SSS} %p [%X{stgEnv}] [%X{userNo}] : %m%n'
}
let config ={
  'appenders': {
    'nuxtfileInfo':{ 
      'type': 'dateFile', 
      'filename': `${LOG_DIR}lego-nuxt_biz.log`,
      'pattern': '.yyyy-MM-dd',
      // 'alwaysIncludePattern':true ,
      'compress': true,
      'keepFileExt':true,
      'layout':LAYOUT
    },
    'nuxtfileWarn':{ 
      'type': 'dateFile', 
      'filename': `${LOG_DIR}lego-nuxt_warn.log`,
      'pattern': '.yyyy-MM-dd',
      // 'alwaysIncludePattern':true ,
      'compress': true,
      'keepFileExt':true,
      'layout':LAYOUT
    },
    'nuxtfileErr':{ 
      'type': 'dateFile', 
      'filename': `${LOG_DIR}lego-nuxt_err.log`,
      'pattern': '.yyyy-MM-dd',
      // 'alwaysIncludePattern':true ,
      'compress': true,
      'keepFileExt':true,
      'layout':LAYOUT_ERROR
    },
  },
  'categories': {
    'default': { 'appenders': ['nuxtfileInfo'], 'level': 'info' },
    'warn': { 'appenders': ['nuxtfileWarn'], 'level': 'warn' },
    'error': { 'appenders': ['nuxtfileErr'], 'level': 'error' }
  }
}
let log4js = require('log4js')
log4js.configure(config)
const infologger = log4js.getLogger()
const warnlogger = log4js.getLogger('warn')
const errlogger = log4js.getLogger('error')

export default ({ store }, inject) => {
  inject('info', (msg) => { 
    infologger.addContext('userNo', store.state.server.userNo)
    infologger.addContext('stgEnv', stgEnv)
    infologger.info(msg)
  })
  inject('warn', (msg) => {  
    warnlogger.addContext('userNo', store.state.server.userNo)
    warnlogger.addContext('stgEnv', stgEnv)
    warnlogger.warn(msg)
  })
  inject('error', (msg) => {  
    errlogger.addContext('userNo', store.state.server.userNo)
    errlogger.addContext('userAgent', store.state.server.userAgent)
    errlogger.addContext('stgEnv', stgEnv)
    errlogger.error(msg)
  })
}